<template>
  <div class="app-container org">
    <el-card class="leftCard">
      <!-- 搜索条件盒子 -->
      <div>
        <p class="pinformation">搜索条件</p>
        <el-form :model="queryParams" ref="queryRef" label-width="80px">
          <el-row>
            <el-col :span="24">
              <el-form-item>
                <el-button type="success" icon="Search" @click="handleQuery">搜索</el-button>
                <div class="resetBtn" style="margin-left: 6px">
                  <el-button icon="Refresh" @click="resetQuery">重置</el-button>
                </div>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
      </div>

      <div>
        <el-row>
          <el-col :span="5">
            <p class="pinformation">仪器技术指标列表</p>
          </el-col>
          <el-col :span="13">
            <p
                style="
                font-size: 14px;
                margin-top: 0px;
                margin-bottom: 6px;
                font-weight: 500;
              "
            >
              共 {{ total }} 条数据
            </p>
          </el-col>
          <el-col :span="3"
          ><el-button type="success" link icon="Plus" @click="handleAdd"
          >新增</el-button
          ></el-col
          >
          <el-col :span="2"
          ><el-button
              type="success"
              link
              icon="Delete"
              @click="handleDelete"
              :disabled="multiple > 0 ? false : true"
          >删除</el-button
          ></el-col
          >
        </el-row>

        <el-table
            max-height="23vw"
            class="business"
            v-loading="loading"
            :highlight-current-row="highLightRow"
            :data="instrumentItemTable"
            @row-click="rowClick"
            @selection-change="handleSelectionChange"
        >
          <el-table-column type="selection" width="55" align="center" />
                  <el-table-column label="仪器id" align="center" prop="instrumentId" />
                  <el-table-column label="检测项目" align="center" prop="jcxmId" />
                  <el-table-column label="准确度" align="center" prop="accuracy" />
                  <el-table-column label="量程" align="center" prop="lc" />
                  <el-table-column label="分辨率" align="center" prop="fbl" />
          <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
            <template #default="scope">
              <el-button link type="success" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['resource:instrumentItem:remove']">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <div class="change">
          <el-pagination
              v-show="total > 0"
              :total="total"
              :page-sizes="[10, 20, 30, 50]"
              v-model:current-page="queryParams.pageNum"
              v-model:page-size="queryParams.pageSize"
              layout="total, sizes, prev, pager, next, jumper"
              @pagination="getList"
              style="margin-top: 20px"
              :pager-count="5"
              @size-change="getList"
              @current-change="getList"
          />
        </div>
      </div>
    </el-card>

    <el-card class="rightCard">
      <!-- 基础信息 -->
      <el-form label-width="130px" :model="form" :rules="rules" ref="instrumentItemRef">
        <div>
          <el-row style="margin-bottom: 5px">
            <el-col :span="22">
              <p class="pinformation">基础信息</p>
            </el-col>
            <el-col :span="2"
            ><el-button type="success" size="small" @click="submitForm">
              保存</el-button
            ></el-col
            >
          </el-row>
              <el-row>
              </el-row>
              <el-row>
                            <el-col :span="24">
                              <el-form-item label="仪器id" prop="instrumentId">
                                <el-input maxlength="100" v-model="form.instrumentId" placeholder="请输入仪器id" clearable/>
                              </el-form-item>
                            </el-col>
              </el-row>
              <el-row>
                            <el-col :span="24">
                              <el-form-item label="检测项目" prop="jcxmId">
                                <el-input maxlength="100" v-model="form.jcxmId" placeholder="请输入检测项目" clearable/>
                              </el-form-item>
                            </el-col>
              </el-row>
              <el-row>
                            <el-col :span="24">
                              <el-form-item label="准确度" prop="accuracy">
                                <el-input maxlength="100" v-model="form.accuracy" placeholder="请输入准确度" clearable/>
                              </el-form-item>
                            </el-col>
              </el-row>
              <el-row>
                            <el-col :span="24">
                              <el-form-item label="量程" prop="lc">
                                <el-input maxlength="100" v-model="form.lc" placeholder="请输入量程" clearable/>
                              </el-form-item>
                            </el-col>
              </el-row>
              <el-row>
                            <el-col :span="24">
                              <el-form-item label="分辨率" prop="fbl">
                                <el-input maxlength="100" v-model="form.fbl" placeholder="请输入分辨率" clearable/>
                              </el-form-item>
                            </el-col>
              </el-row>
        </div>
      </el-form>




    </el-card>

  </div>
</template>

<script setup name="InstrumentItem">
import { listInstrumentItem, getInstrumentItem, delInstrumentItem, addInstrumentItem} from "@/api/resource/instrumentItem";

const { proxy } = getCurrentInstance();


const instrumentItemList = ref([]);

const loading = ref(true);
const ids = ref([]);
const multiple = ref(false);
const disabled = ref(false);
const total = ref(0);
const instrumentItemTable = ref([]);
const instrumentItemForm = ref({});
const highLightRow = ref(true);




const data = reactive({
  form: {
  },
  queryParams: {
    pageNum: 1,
    pageSize: 10,
  },
  rules: {
                  instrumentId: [
              { required: true, message: "仪器id不能为空", trigger: "blur" }
            ],
                  jcxmId: [
              { required: true, message: "检测项目不能为空", trigger: "blur" }
            ],
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询仪器技术指标列表 */
function getList() {
  loading.value = true;
  listInstrumentItem(queryParams.value).then(response => {
          instrumentItemTable.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 表单重置
function reset() {
  form.value = {
                  itemId: null,
                  instrumentId: null,
                  jcxmId: null,
                  accuracy: null,
                  lc: null,
                  fbl: null
  };
  proxy.resetForm("instrumentItemRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.itemId);
  multiple.value = selection.length;
}


/** 修改按钮操作 */
function rowClick(row) {
  reset();
  const _itemId = row.itemId || ids.value
  getInstrumentItem(_itemId).then(response => {
    form.value = response.data;
  });
}


/** 提交按钮 */
function submitForm() {
  proxy.$refs["instrumentItemRef"].validate(valid => {
    if (valid) {
      addInstrumentItem(form.value).then(res => {
        proxy.$modal.msgSuccess(res.msg);
        getList();
      });
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _itemIds = row.itemId || ids.value;
  proxy.$modal.confirm('是否确认删除仪器技术指标编号为"' + _itemIds + '"的数据项？').then(function() {
    return delInstrumentItem(_itemIds);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('resource/instrumentItem/export', {
    ...queryParams.value
  }, `instrumentItem_${new Date().getTime()}.xlsx`)
}

getList();
</script>
